﻿using System.ComponentModel.DataAnnotations;
using FluentValidationExample.Application.Products.Contracts;

namespace FluentValidationExample.Application.Products.ModelValidations;

/// <summary>
/// 产品最低价格验证
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class ProductLowerPriceAttribute : ValidationAttribute
{
    public ProductLowerPriceAttribute(int minPrice)
        => MinPrice = minPrice;

    public int MinPrice { get; }

    protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
    {
        var createProductRequest = (CreateProductRequest)validationContext.ObjectInstance;
        if (createProductRequest.Price <= MinPrice)
        {
            return new ValidationResult($"价格不能小于{MinPrice}");
        }

        return ValidationResult.Success;
    }
}